package net.vkits.anjiamiao.application;

import net.vkits.anjiamiao.domain.model.Complain;
import net.vkits.anjiamiao.domain.model.ComplainStatus;
import net.vkits.anjiamiao.domain.model.Device;
import net.vkits.anjiamiao.domain.model.Message;
import net.vkits.anjiamiao.domain.repository.ComplainRepository;
import net.vkits.anjiamiao.domain.repository.DeviceRepository;
import net.vkits.anjiamiao.domain.repository.MessageRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by ethan-liu on 2016/11/19.
 */
@Service
public class ComplainApplicationService {
    private static final Logger logger = LoggerFactory.getLogger(ComplainApplicationService.class);

    @Resource
    private ComplainRepository complainRepository;
    @Resource
    private DeviceRepository deviceRepository;
    @Resource
    private MessageRepository messageRepository;
    @Resource
    private ApplicationContext applicationContext;

    @Transactional
    public void submitComplain(String content, String cellphone) {
        Complain complain = complainRepository.save(new Complain(cellphone, content));
        Set<Device> devices = deviceRepository.findByCellphone(complain.getCellphone());
        Set<String> umDeviceTokens = new HashSet<>();
        devices.forEach(device -> umDeviceTokens.add(device.getUmDeviceToken()));
        Message message = new Message();
        message.setComplainStatus(complain.getStatus());
        message.setTitle("申诉进度通知");
        message.setSender("system");
        message.setStartDate(complain.getSubmitDate());
        message.setEndDate(complain.getPromiseDate().toString());
        message.setUmDeviceTokens(umDeviceTokens);
        message.setCellPhoneNum(complain.getCellphone());
        Message m = messageRepository.save(message);
        applicationContext.publishEvent(m);
    }

    public Complain findById(String id) {
        return complainRepository.findOne(Long.parseLong(id));
    }

    @Transactional
    public void changeStatus(String id, String status) {
        Complain complain = complainRepository.findOne(Long.parseLong(id));
        Set<Device> devices = deviceRepository.findByCellphone(complain.getCellphone());
        Set<String> umDeviceTokens = new HashSet<>();
        devices.forEach(device -> umDeviceTokens.add(device.getUmDeviceToken()));
        ComplainStatus complainStatus = ComplainStatus.valueOf(status);
        Message message = new Message();
        message.setComplainStatus(complainStatus);
        message.setTitle("申诉进度通知");
        message.setSender("system");
        message.setStartDate(complain.getSubmitDate());
        message.setUmDeviceTokens(umDeviceTokens);
        message.setCellPhoneNum(complain.getCellphone());
        if (complainStatus.equals(ComplainStatus.Accept)) {
            complain.setAcceptDate();
            message.setEndDate(complain.getAcceptDate());
        } else if (complainStatus.equals(ComplainStatus.Unaccepted)) {
            complain.setUnacceptedDate();
            message.setEndDate(complain.getUnacceptedDate());
        }
        complain.setStatus(complainStatus);
        Message m = messageRepository.save(message);
        applicationContext.publishEvent(m);
        complainRepository.save(complain);
    }

    public Page<Complain> getComplains(Pageable pageable) {
        return complainRepository.findAll(pageable);
    }
}
